<template>
  <view class="mine-container">
	  <status-bar bgColor="#fff"></status-bar>
    <!-- 用户信息区域 -->
    <view class="user-info">
      <image v-if="avatar"  class="avatar" :src="avatar" @click="goToChooseAvatar"></image>
      <image v-else  class="avatar" src="static/logo.png" @click="goToChooseAvatar"></image>
      <text class="phone-number" @click="openNicknameDialog">{{ nickName }}
        <!-- <text class="edit-icon">编辑</text> -->
      </text>
    </view>

    <!-- 等级信息区域已隐藏 -->
    
    <!-- 信用值和好评区域已隐藏 -->

    <!-- 功能卡片区域 -->
    <view class="card-container">
      <view class="function-cards">
        <view class="function-card" @click="goToWallet">
          <view class="card-icon wallet-icon">
            <image src="/static/images/icon/wallet.png" class="icon-img"></image>
          </view>
          <view class="card-content">
            <text class="card-title">我的钱包</text>
            <text class="card-desc">申请提现</text>
          </view>
        </view>
        <view class="function-card" @click="goToDataStatistics">
          <view class="card-icon stats-icon">
            <image src="/static/images/icon/statistics.png" class="icon-img"></image>
          </view>
          <view class="card-content">
            <text class="card-title">数据统计</text>
            <text class="card-desc">收入单量统计</text>
          </view>
        </view>
        <!-- <view class="function-card" @click="goToBill">
          <view class="card-icon bill-icon">
            <image src="/static/images/icon/statistics.png" class="icon-img"></image>
          </view>
          <view class="card-content">
            <text class="card-title">我的账单</text>
            <text class="card-desc">月度年度账单</text>
          </view>
        </view> -->
      </view>
    </view>

    <!-- 菜单列表区域 -->
    <view class="menu-list" v-if="false">
      <view class="list-cell list-cell-arrow" @click="goToPerfectInfo">
        <view class="menu-item-box">
          <text class="menu-icon iconfont icon-process"></text>
          <text>资料完善</text>
        </view>
      </view>

      <view class="list-cell list-cell-arrow">
        <view class="menu-item-box">
          <text class="menu-icon iconfont icon-safety"></text>
          <text>施工安全规定</text>
        </view>
      </view>
      <view class="list-cell list-cell-arrow">
        <view class="menu-item-box">
          <text class="menu-icon iconfont icon-rule"></text>
          <text>奖惩规则</text>
        </view>
      </view>
      <view class="list-cell list-cell-arrow">
        <view class="menu-item-box">
          <text class="menu-icon iconfont icon-process"></text>
          <text>接单流程</text>
        </view>
      </view>
    </view>

    <!-- 设置菜单 -->
    <view class="menu-list">
      <view class="list-cell list-cell-arrow" @click="goToSetting">
        <view class="menu-item-box">
          <text class="menu-icon iconfont icon-settings"></text>
          <text>设置</text>
        </view>
      </view>
    </view>

    <!-- 底部占位，防止内容被tabbar遮挡 -->
    <view class="tabbar-placeholder"></view>

    <!-- 使用自定义tabbar -->
    <custom-tabbar :active="2"></custom-tabbar>

    <!-- 昵称修改弹窗 -->
    <view class="nickname-dialog" v-if="showNicknameDialog">
      <view class="dialog-mask" @click="closeNicknameDialog"></view>
      <view class="dialog-content">
        <view class="dialog-header">
          <text class="dialog-title">修改昵称</text>
          <text class="dialog-close" @click="closeNicknameDialog">×</text>
        </view>
        <view class="dialog-body">
          <uni-easyinput type="text" v-model="newNickname" placeholder="请输入新昵称" focus />
        </view>
        <view class="dialog-footer">
          <view class="dialog-btn cancel-btn" @click="closeNicknameDialog">取消</view>
          <view class="dialog-btn confirm-btn" @click="confirmNickname">确定</view>
        </view>
      </view>
    </view>
  </view>


</template>

<script>
import { updateNickName } from '@/api/system/user.js'
export default {
  data() {
    return {
      avatar: this.$store.state.master.masterAvatar,
      nickName: this.$store.state.master.masterName,
      showNicknameDialog: false, // 控制昵称修改弹窗显示
      newNickname: '' // 新昵称
    }
  },
  onShow() {
    
    this.avatar = this.$store.state.master.masterAvatar;
    this.nickName = this.$store.state.master.masterName;
    console.log(this.avatar);
    console.log(this.nickName);

    uni.hideTabBar()	
  },
  computed: {
    windowHeight() {
      return uni.getSystemInfoSync().windowHeight - 50
    }
  },
  methods: {
    goToChooseAvatar() {
      uni.navigateTo({
        url: '/pages/mine/avatar'
      });
    },
    goToSetting() {
      uni.navigateTo({
        url: '/pages/mine/setting'
      });
    },
    goToPerfectInfo() {
      uni.navigateTo({
        url: '/pages/perfectInfo'
      });
    },
    
    goToWallet() {
      uni.navigateTo({
        url: '/pages/mine/wallet'
      });
    },
    
    goToCreditRecord() {
      uni.navigateTo({
        url: '/pages/mine/credit-record'
      });
    },
    
    goToDataStatistics() {
      uni.navigateTo({
        url: '/pages/mine/data-statistics'
      });
    },
    
    goToBill() {
      uni.navigateTo({
        url: '/pages/mine/bill'
      });
    },

    // 打开昵称修改弹窗
    openNicknameDialog() {
      return;
      this.newNickname = this.nickName;
      this.showNicknameDialog = true;
    },

    // 关闭昵称修改弹窗
    closeNicknameDialog() {
      this.showNicknameDialog = false;
    },

    // 确认修改昵称
    confirmNickname() {
      if (!this.newNickname.trim()) {
        return uni.showToast({ title: '昵称不能为空', icon: 'none' });
      }

      updateNickName({
        nickName: this.newNickname,
      }).then(res => {
        console.log(res);
        // 更新昵称
        this.nickName = this.newNickname.trim();

        // 更新到store
        this.$store.commit('SET_NICKNAME', this.nickName);

        // 关闭弹窗
        this.closeNicknameDialog();

        // 提示成功
        uni.showToast({ title: '昵称修改成功', icon: 'success' });

      });


    }
  }
}
</script>

<style lang="scss" scoped>
.mine-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 30rpx;
}

/* 用户信息区域 */
.user-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40rpx 0;
  background-color: #fff;
}

.avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  background-color: #e1f1ff;
  margin-bottom: 20rpx;
}

.phone-number {
  font-size: 32rpx;
  color: #999;
  position: relative;
  padding-right: 60rpx;
}

.edit-icon {
  font-size: 24rpx;
  color: #2196f3;
  margin-left: 10rpx;
}

/* 昵称修改弹窗样式 */
.nickname-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
}

.dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.dialog-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
}

.dialog-header {
  position: relative;
  padding: 30rpx;
  text-align: center;
  border-bottom: 1px solid #eee;
}

.dialog-title {
  font-size: 32rpx;
  font-weight: 500;
}

.dialog-close {
  position: absolute;
  right: 30rpx;
  top: 30rpx;
  font-size: 40rpx;
  color: #999;
}

.dialog-body {
  padding: 30rpx;
}

.dialog-footer {
  display: flex;
  border-top: 1px solid #eee;
}

.dialog-btn {
  flex: 1;
  height: 90rpx;
  line-height: 90rpx;
  text-align: center;
  font-size: 32rpx;
}

.cancel-btn {
  color: #666;
  border-right: 1px solid #eee;
}

.confirm-btn {
  color: #2196f3;
}

/* 等级卡片 */
.level-card {
  margin: 20rpx 30rpx;
  padding: 30rpx;
  background: linear-gradient(to right, #d9a282, #e8c4b3);
  border-radius: 12rpx;
  color: #fff;
}

.level-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.level-title {
  font-size: 34rpx;
  font-weight: bold;
}

.level-action {
  font-size: 28rpx;
  display: flex;
  align-items: center;
}

.arrow {
  margin-left: 10rpx;
}

.level-progress {
  position: relative;
  margin-bottom: 20rpx;
}

.progress-text {
  position: absolute;
  left: 0;
  top: -30rpx;
  font-size: 24rpx;
}

.progress-bar {
  height: 10rpx;
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 10rpx;
}

.progress-inner {
  height: 100%;
  background-color: #fff;
  border-radius: 10rpx;
}

.level-desc {
  font-size: 24rpx;
  text-align: right;
}

/* 信用值和好评区域 */
.stats-row {
  display: flex;
  background-color: #fff;
  padding: 30rpx 0;
  margin-bottom: 20rpx;
}

.stats-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stats-value {
  font-size: 40rpx;
  color: #2196f3;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.stats-label {
  font-size: 28rpx;
  color: #666;
}

/* 功能卡片区域 */
.card-container {
  margin: 20rpx 30rpx;
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  margin-bottom: 20rpx;
}

.function-cards {
  display: flex;
  padding: 30rpx;
}

.function-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20rpx;
}

.wallet-icon {
  background-color: #e6f2ff;
  color: #2196f3;
}

.stats-icon {
  background-color: #e6f2ff;
  color: #2196f3;
}

.card-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 10rpx;
}

.card-desc {
  font-size: 24rpx;
  color: #999;
}

/* 菜单列表 */
.menu-list {
  margin: 20rpx 30rpx;
  border-radius: 12rpx;
  overflow: hidden;
}

/* 图标样式 */
.iconfont {
  font-size: 40rpx;
}

.icon-img {
  width: 40rpx;
  height: 40rpx;
}

.icon-safety,
.icon-rule,
.icon-process,
.icon-settings {
  font-size: 36rpx;
  margin-right: 20rpx;
  color: #999;
}
</style>